package cn.com.anysdk.redis.demo.controller;

import cn.com.anysdk.starter.config.redis.RedisProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * Redis配置展示控制器
 * 用于展示当前Redis配置信息，支持敏感信息脱敏
 */
@RestController
@RequestMapping("/api/redis")
public class RedisConfigController {

    @Autowired
    private RedisProperties redisProperties;

    /**
     * 获取Redis配置信息
     * @return 配置信息（敏感信息已脱敏）
     */
    @GetMapping("/config")
    public Map<String, Object> getRedisConfig() {
        Map<String, Object> result = new HashMap<>();
        result.put("currentEnvironment", redisProperties.getEnvironment());
        result.put("timestamp", System.currentTimeMillis());

        // 本地Redis配置
        result.put("local", buildLocalConfig());

        // 阿里云Redis配置
        result.put("aliyun", buildAliyunConfig());

        // 华为云Redis配置
        result.put("huawei", buildHuaweiConfig());

        // 腾讯云Redis配置
        result.put("tencent", buildTencentConfig());

        return result;
    }

    /**
     * 获取当前环境的Redis配置
     * @return 当前环境的详细配置
     */
    @GetMapping("/current")
    public Map<String, Object> getCurrentConfig() {
        Map<String, Object> result = new HashMap<>();
        result.put("environment", redisProperties.getEnvironment());
        result.put("timestamp", System.currentTimeMillis());

        String env = redisProperties.getEnvironment();
        switch (env) {
            case "local":
                result.put("config", buildLocalConfig());
                break;
            case "aliyun":
                result.put("config", buildAliyunConfig());
                break;
            case "huawei":
                result.put("config", buildHuaweiConfig());
                break;
            case "tencent":
                result.put("config", buildTencentConfig());
                break;
            default:
                result.put("config", buildLocalConfig());
                result.put("warning", "Unknown environment, fallback to local");
                break;
        }

        return result;
    }

        private Map<String, Object> buildLocalConfig() {
        Map<String, Object> config = new HashMap<>();
        RedisProperties.LocalConfig local = redisProperties.getLocal();

        config.put("host", local.getHost());
        config.put("port", local.getPort());
        config.put("database", local.getDatabase());
        config.put("password", maskPassword(local.getPassword()));

        return config;
    }

    private Map<String, Object> buildAliyunConfig() {
        Map<String, Object> config = new HashMap<>();
        RedisProperties.AliyunConfig aliyun = redisProperties.getAliyun();

        config.put("endpoint", aliyun.getConfig() != null ? aliyun.getConfig().getEndpoint() : null);
        config.put("port", 6379);
        config.put("mode", aliyun.getMode());

        return config;
    }

    private Map<String, Object> buildHuaweiConfig() {
        Map<String, Object> config = new HashMap<>();
        cn.com.anysdk.redis.config.HuaweiRedisConfig huawei = redisProperties.getHuawei();

        config.put("endpoint", huawei.getEndpoint());
        config.put("port", huawei.getPort());
        config.put("accessKey", maskAccessKey(huawei.getAccessKey()));
        config.put("secretKey", maskSecretKey(huawei.getSecretKey()));
        config.put("region", huawei.getRegion());
        config.put("mode", huawei.getMode());

        return config;
    }

    private Map<String, Object> buildTencentConfig() {
        Map<String, Object> config = new HashMap<>();
        cn.com.anysdk.redis.config.TencentRedisConfig tencent = redisProperties.getTencent();

        config.put("endpoint", tencent.getEndpoint());
        config.put("port", tencent.getPort());
        config.put("secretId", maskSecretId(tencent.getSecretId()));
        config.put("secretKey", maskSecretKey(tencent.getSecretKey()));
        config.put("region", tencent.getRegion());
        config.put("mode", tencent.getMode());

        return config;
    }

    /**
     * 脱敏处理密码
     */
    private String maskPassword(String password) {
        if (password == null || password.length() <= 4) {
            return "****";
        }
        return password.substring(0, 2) + "****" + password.substring(password.length() - 2);
    }

    /**
     * 脱敏处理AccessKey
     */
    private String maskAccessKey(String accessKey) {
        if (accessKey == null || accessKey.length() <= 6) {
            return "******";
        }
        return accessKey.substring(0, 3) + "******" + accessKey.substring(accessKey.length() - 3);
    }

    /**
     * 脱敏处理SecretKey
     */
    private String maskSecretKey(String secretKey) {
        if (secretKey == null || secretKey.length() <= 8) {
            return "********";
        }
        return secretKey.substring(0, 4) + "********" + secretKey.substring(secretKey.length() - 4);
    }

    /**
     * 脱敏处理SecretId
     */
    private String maskSecretId(String secretId) {
        if (secretId == null || secretId.length() <= 8) {
            return "********";
        }
        return secretId.substring(0, 4) + "********" + secretId.substring(secretId.length() - 4);
    }
}